// pagesA/sjstatistics/sjstatistics.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istype:wx.getStorageSync('staffInfoIstype'),
    nyinit: 0,
    disabled: true,
    date: '',
    dateNo:"",
    responsible: '',
    responsibleOptions: [],
    responsibleList: [],
    dataList: [],
    userInfo: null,
    orderData: null,
    queryFrom: {
      // "hospitalDepartId": 0,
      // "hospitalId": 0,
      // "year": "",
      // "yearAndMonth": ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: app.globalData.userInfo,
      date: this.fromaTime(new Date()),
      dateNo:this.fromaTimes(new Date())
    })
    this.getTyoeList()
    // this.getPT()
    this.getPE()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log(app.globalData.userInfo);
  },
  getTyoeList(){
    let that = this
    console.log(that.data.istype);

    wx.request({
      url: app.globalData.serverUrl + '/st/sort/'+that.data.istype,
      method: 'get',
      header: {
        'token': wx.getStorageSync('token')
      },
      success:res=>{
        let resp = res.data
        if(resp.code==200){
          resp.data.hospitalList.map(item=>{
            item.value = item.hospitalId
            item.label = item.hospitalName
            if(item.hospitalId==that.data.userInfo.hospitalId){
              item.checked = true
            }
          })
          that.setData({
            responsibleList:resp.data.hospitalList,
          })
          that.getList()
        }
      }
    })
  },
  getList() {
    let that = this
    let newData={}
    wx.showLoading({
      title: '',
      mask:true
    })
    newData.type = that.data.istype
    if(that.data.nyinit==1){
      newData.year = that.data.dateNo
    }else{
      newData.yearAndMonth = that.data.dateNo
    }
    if(that.data.queryFrom.hospitalId){
      newData.hospitalId = that.data.queryFrom.hospitalId
    }
    if(that.data.queryFrom.hospitalDepartId){
      newData.hospitalDepartId = that.data.queryFrom.hospitalDepartId
    }
    wx.request({
      url: app.globalData.serverUrl + '/statistics/count',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.dataStatistics.istab = true
          resData.data.dataStatistics.hospitalId = newData.hospitalId==undefined?that.data.userInfo.hospital.hospitalId:newData.hospitalId
          that.setData({
            orderData: resData.data.dataStatistics
          })
          that.data.responsibleList.map(item=>{
            if((newData.hospitalId==undefined?that.data.userInfo.hospital.hospitalId:newData.hospitalId) == item.hospitalId){
              that.setData({
                'orderData.runningDays' : item.runningDays
              })
            }
          })
          wx.hideLoading()
        }else{
          wx.hideLoading()
        }
      })
    })
  },
  getPE(){
    let that = this
    let urls = that.data.queryFrom.hospitalId?'/hosPart/list/by/'+ that.data.queryFrom.hospitalId:'/medical/all/depart'
    wx.request({
      url: app.globalData.serverUrl + urls,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.hospitalDepartList.map((item,index)=>{
            item.value = item.deptId
            item.label = item.deptName
            if(index==0){
              item.checked = true
            }
          })
          that.setData({
            responsibleOptions:resData.data.hospitalDepartList,
            'queryFrom.hospitalDepartId':resData.data.hospitalDepartList[0].deptId
          })
          // this.getList()
        }
      })
    })
  },
  getPT(){
    let that = this
    wx.request({
      url: app.globalData.serverUrl + '/hospital/list',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          console.log(resData.data);
          resData.data.hospitalList.map(item=>{
            item.value = item.hospitalId
            item.label = item.hospitalName
          })
          that.setData({
            responsibleList:resData.data.hospitalList
          })
        }
      })
    })
  },
  bindChangeResponsible(e) {
    this.setData({
      responsible: e.detail.label,
      'queryFrom.hospitalDepartId':e.detail.value
    })
    this.getList()
  },
  bindChangeResponsiblejd(e){
    console.log(e.detail.value);
    this.setData({
      responsible: e.detail.label,
      'queryFrom.hospitalId':e.detail.value,
      'queryFrom.hospitalDepartId':''
    })
    this.getList()
    this.getPE()
  },
  syncCount(e) {
    console.log(e.detail.value);
    let that = this
    if(e.detail.value=='responsible'){
      const id = e.detail.value
      console.log(e.detail);
      this.selectComponent('#' + id).show()
    }else{
      wx.navigateTo({
        url: '/pagesB/statisticsquerys/statisticsquerys?id='+e.detail.value+'&&istype='+that.data.istype,
      })
    }
  },
  synctabCount(e){
    console.log(e.detail.value);
    this.setData({
      istype: e.detail.value,
      queryFrom:{}
    })
    this.getTyoeList()
  },

  tabNY() {
    if (this.data.nyinit == 0) {
      this.setData({
        nyinit: 1,
      })
    } else {
      this.setData({
        nyinit: 0,
      })
    }
    this.setData({
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
    this.getList()
  },
  bindDateChange: function (e) {
    let str = e.detail.value
    if (str.indexOf('-') == -1) {
      console.log(str.indexOf('-'));
      str += '年'
    } else {
      str = str.replace(/-/g, '年') + '月'
    }
    console.log(e, 'picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      date: str,
      dateNo:e.detail.value
    })
    this.getList()
  },
  fromaTime(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '年' + month + '月'
    } else {
      return year + '年'
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  fromaTimes(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '-' + (month<10?('0'+month):month)
    } else {
      return year+''
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      istyp:null,
    })
    this.getList()
    this.getPE()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})